# coding:utf-8
'''
股票
http://hq.sinajs.cn/list=sh601003
http://hq.sinajs.cn/list=sh601002

分时K线图
http://image.sinajs.cn/newchart/min/n/sh000001.gif
'''
import urllib3
from urllib3 import PoolManager
import pandas as pd

http = PoolManager()
urllib3.disable_warnings()

# 创业板
stock_codes = ["sz300001", "sz300002", "sz300003", "sz300004", "sz300005"]
# 科创板
# stock_codes = ["sh688001", "sh688002", "sh688003", "sh688005", "sh688006"]
stock_items = []

for stock_code in stock_codes:
    url = "http://hq.sinajs.cn/list=" + stock_code
    result = http.request(method="GET", url=url)
    text = result.data.decode("gbk").strip()
    '''
    0:大秦铁路，股票
    1:27.55，今日开盘价
    2:27.25，昨日收盘价
    3:26.91，当前价格
    4:27.55，今日最高价
    5:26.20，今日最低价
    30:2008-01-11，日期
    31:15:05:32，时间
    '''
    items = text[text.find("\"") + 1:-2].split(",")
    if len(items) > 0:
        stock_items.append([stock_code, url, "http://image.sinajs.cn/newchart/min/n/%s.gif" % (stock_code), items[30], items[31],
                            items[0], float(items[1]), float(items[2]), float(items[3]), float(items[4]), float(items[5])])  
df = pd.DataFrame(stock_items, columns=["代码", "接口地址", "K线图", "日期", "时间",
                                        "名称", "今日开盘价", "昨日收盘价", "当前价格", "今日最高价", "今日最低价"])
df.to_excel("d:/stock.xlsx")
